package com.example.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.example.dto.Customer;

public class CustomerDao {

	Connection connection = null;

	Statement statement = null;

	public Customer loadCustomer(String loginId, String password)

	{

		ResultSet resultSet = null;
		Customer custmomer = null;
		try {
			// This will load the MySQL driver, each DB has its own driver
			Class.forName("com.mysql.jdbc.Driver");
			// Setup the connection with the DB
			connection = DriverManager
					.getConnection("jdbc:mysql://localhost/classicmodels?"
							+ "user=root&password=root");

			// Statements allow to issue SQL queries to the database
			statement = connection.createStatement();
			// Result set get the result of the SQL query
			resultSet = statement
					.executeQuery("select * from customers where loginid ='"
							+ loginId + "' and password='" + password + "'");

			while (resultSet.next()) {
				custmomer = new Customer();
				custmomer.setCustomerNumber(resultSet
						.getString("customerNumber"));
				custmomer.setFirstName(resultSet.getString("contactFirstName"));
				custmomer.setLastName(resultSet.getString("contactLastName"));

				break;

			}

		} catch (Exception exception) {

		} finally {
			try {
				resultSet.close();

				connection.close();
				statement.close();

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return custmomer;

	}
	
	 
	
	
	
	

	public static void main(String[] args) {

		CustomerDao customerDao = new CustomerDao();
		System.out.println(customerDao.loadCustomer("cust1", "cust1")
				.getFirstName());

	}
}
